//
//  BaseTableView.m
//  TableView
//
//  Created by Maple on 13-7-1.
//  Copyright (c) 2013年 ci123. All rights reserved.
//

#import "BaseTableView.h"

@implementation BaseTableView

#define LIMIT_PAGE_NUM  30
#define LIMIT_AUTOLOAD_MAXNUM   300
#define AUTOLOAD_FROM_BOTTOM    30

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self _initView];
        self.isheaderView = YES;
        self.needAutoModifiedByData = YES;
        self.isAutoload = YES;
        self.isfooterView = YES;
        self.isLoadingMore = NO;
        self.pageNum = LIMIT_PAGE_NUM;
        _data = [NSArray array];
    }
    return self;
}

- (void)_initView {
    
    if (_refreshHeaderView == nil) {
        EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.bounds.size.height, self.frame.size.width, self.bounds.size.height)];
        self.delegate = self;
        self.dataSource = self;
        view.delegate = self;
        _refreshHeaderView = view;
    }
    if(_loadingView == nil){
        _loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, APP_SCREEN_WIDTH, 100)];
        _loadingView.backgroundColor = [UIColor clearColor];
        _loadingButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 15, APP_SCREEN_WIDTH, 40)];
        _loadingButton.backgroundColor = [UIColor whiteColor];
        _loadingButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_loadingButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_loadingButton addTarget:self action:@selector(loadingMore) forControlEvents:UIControlEventTouchUpInside];
        [_loadingButton setTitle:@"显示更多" forState:UIControlStateNormal];
        [_loadingButton setTitle:@"加载中..." forState:UIControlStateSelected | UIControlStateDisabled];
        [_loadingView addSubview:_loadingButton];

        _loadingActivity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _loadingActivity.frame = CGRectMake(100, 35, 0, 0);
        _loadingActivity.hidesWhenStopped = YES;
        [_loadingView addSubview:_loadingActivity];

        self.tableFooterView = _loadingView;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (_needAutoModifiedByData) {
        if(!_isfooterView || [_data count] < _pageNum) {
            _loadingView.hidden = YES;
            _isAutoload = NO;
        }else {
            _loadingView.hidden = NO;
            _isAutoload = YES;
        }
    }else {
        if (_isfooterView) {
            _loadingView.hidden = NO;
        }
    }
}

- (void)setIsheaderView:(BOOL)isheaderView{
  if(!isheaderView){
    if([_refreshHeaderView superview]){
      [_refreshHeaderView removeFromSuperview];
      _refreshHeaderView = nil;
    }
  }else{
    [self addSubview:_refreshHeaderView];
  }
}

- (void)loadingMore
{
    [_loadingActivity startAnimating];
    _loadingButton.selected = YES;
    [_loadingButton setEnabled:NO];
    if([self.egoFreshDelegate respondsToSelector:@selector(loadMore:)]) {
        [self.egoFreshDelegate loadMore:self];
    }
}

- (void)doneLoadingMore:(BOOL)isHidden
{
    [_loadingActivity stopAnimating];
    _loadingButton.selected = NO;
    [_loadingButton setEnabled:YES];
    if(isHidden){
        _isAutoload = NO;
        _loadingView.hidden = YES;
    }else{
        _loadingView.hidden = NO;
    }
}

- (void)refreshLoading
{
  [_refreshHeaderView refreshLoading:self];
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_data count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  if(self.egoFreshDelegate && [self.egoFreshDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
    [self.egoFreshDelegate tableView:self didSelectRowAtIndexPath:indexPath];
  }
}

#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
  _reloading = YES;
  
}

- (void)doneLoadingTableViewData{
  _reloading = NO;
  [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self];
}


#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(_isAutoload && _isfooterView && !_isLoadingMore) {//自动加载
        if((scrollView.contentOffset.y >= fmaxf(.0f, scrollView.contentSize.height - scrollView.frame.size.height) - AUTOLOAD_FROM_BOTTOM) && (scrollView.contentSize.height > APP_SCREEN_HEIGHT)) {
            _isLoadingMore = YES;
            [self loadingMore];
        }
    }
    
    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (self.egoFreshDelegate && [self.egoFreshDelegate respondsToSelector:@selector(tableViewBeginDragging:)]) {
        [self.egoFreshDelegate tableViewBeginDragging:scrollView];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_isLoadingMore) {
        _isLoadingMore = NO;
    }
}

#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods
//下拉到一定距离，手指放开时调用
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view
{
  
  [self reloadTableViewDataSource];
  
  if(self.egoFreshDelegate && [self.egoFreshDelegate respondsToSelector:@selector(pullDown:)]) {
    [self.egoFreshDelegate pullDown:self];
  }
  
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view
{
  
  return _reloading; // should return if data source model is reloading
  
}

//取得下拉刷新的时间
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
  
  return [NSDate date]; // should return date data source was last changed
  
}


@end
